﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Database;
using KPP.Properties;

namespace KPP
{
    public partial class RegisterPassForm : Form
    {
        private const int ButtonsOffset = 80;
        private const int LabelOffset = 30;
        private const int PassOffset = 50;

        private readonly int _initialHeight;

        public RegisterPassForm()
        {
            InitializeComponent();

            ArrangeItems();

            SetDefaults();

            comboBox1.SelectedIndex = 0;
            typeOfPassBox.SelectedIndex = 0;

            _initialHeight = Size.Height;

            InitializeConnection();
        }

        private void SetDefaults()
        {
            numberTextBox.SetDefaults();
            seriesTextBox.SetDefaults();
            dormTextBox.SetDefaults();

            numberTextBox.DefaultText = Resources.defautlPassNumber;
            seriesTextBox.DefaultText = Resources.defaultPassSeries;
            dormTextBox.DefaultText = "21";
        }

        private static void InitializeConnection()
        {
            DatabaseHelper.Connect();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ArrangeItems()
        {
            applyButton.Location = new Point(applyButton.Location.X, Size.Height - ButtonsOffset);
            newPersonButton.Location = new Point(newPersonButton.Location.X, Size.Height - ButtonsOffset);
            closeButton.Location = new Point(closeButton.Location.X, Size.Height - ButtonsOffset);
        }

        private void DeactivateControls()
        {
            newPersonButton.Visible = false;
            label1.Visible = false;
            label2.Visible = false;

            if (Size.Height != _initialHeight && !dormLabel.Visible)
            {
                Size = new Size(Size.Width, Size.Height - LabelOffset);
                ArrangeItems();
            }
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            if (!numberTextBox.HasText || !seriesTextBox.HasText)
            {
                MessageBox.Show(Resources.NoDataError, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int passSeries;
            bool parseResult = int.TryParse(seriesTextBox.Text, out passSeries);

            if (!parseResult)
            {
                MessageBox.Show(Resources.PassSeriesError, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int passNumber; 
            parseResult = int.TryParse(numberTextBox.Text, out passNumber);

            if (!parseResult)
            {
                MessageBox.Show(Resources.PassNumberError, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int personID = DatabaseHelper.GetPerson(passSeries, passNumber);

            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    if (personID == -1)
                    {
                        Size = new Size(Size.Width, Size.Height + LabelOffset);
                        ArrangeItems();
                        newPersonButton.Visible = true;
                        label1.Visible = true;
                        label2.Visible = true;
                    }
                    else
                    {
                        if (DatabaseHelper.HasActivePass(personID))
                        {
                            MessageBox.Show(Resources.ActivePassError, Resources.Error, MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                            return;
                        }

                        if (dormLabel.Visible == false)
                        {
                            Size = new Size(Size.Width, Size.Height + PassOffset);
                            ArrangeItems();
                            dormLabel.Visible = true;
                            dormTextBox.Visible = true;
                            typeOfPassBox.Visible = true;
                            typePassLabel.Visible = true;
                        }
                        else
                        {
                            bool isTemporary = typeOfPassBox.SelectedIndex == 1;
                            string dorm = dormTextBox.Text;

                            var pass = new Pass(isTemporary, dorm);
                            int passID = DatabaseHelper.InsertNewPass(pass);
                            
                            DatabaseHelper.InsertPassPerson(personID, passID);
                            
                            MessageBox.Show(Resources.PassNumberSucces + pass.Number, Resources.Success, MessageBoxButtons.OK);

                            Size = new Size(Size.Width, _initialHeight);
                            ArrangeItems();
                            dormLabel.Visible = false;
                            dormTextBox.Visible = false;
                            typeOfPassBox.Visible = false;
                            typePassLabel.Visible = false;
                            DeactivateControls();                           
                        }
                    }
                    break;
                case 1:
                    if (personID == -1)
                    {
                        MessageBox.Show(Resources.InvalidUserError, Resources.Error, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                    else
                    {
                        Pass passToDelete = DatabaseHelper.GetPassToUnblock(personID);
                        if (passToDelete != null)
                        {
                            DatabaseHelper.DeletePass(passToDelete.Number);
                        }

                        Pass pass = DatabaseHelper.GetPassToBlock(personID);

                        if (pass == null)
                        {
                            MessageBox.Show(Resources.NoActivePassError, Resources.Error, MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                        }
                        else
                        {
                            var passInformationForm = new PassInformation(pass, true);
                            passInformationForm.Visible = true;
                        }
                    }
                    break;
                case 2:
                    if (personID == -1)
                    {
                        MessageBox.Show(Resources.InvalidUserError, Resources.Error, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                    else
                    {
                        if (DatabaseHelper.HasActivePass(personID))
                        {
                            MessageBox.Show(Resources.ActivePassError, Resources.Error, MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                            return;
                        }

                        Pass pass = DatabaseHelper.GetPassToUnblock(personID);

                        if (pass == null)
                        {
                            MessageBox.Show(Resources.BlockedPassError, Resources.Error, MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                        }
                        else
                        {
                            var passInformationForm = new PassInformation(pass, false);
                            passInformationForm.Visible = true;
                        }
                    }
                    break;
            }
        }

        private void seriesTextBox_TextChanged(object sender, EventArgs e)
        {
            DeactivateControls();
        }

        private void numberTextBox_TextChanged(object sender, EventArgs e)
        {
            DeactivateControls();
        }

        private void newPersonButton_Click(object sender, EventArgs e)
        {
            string passNumber = numberTextBox.Text;
            string passSeries = seriesTextBox.Text;

            var registerForm = new RegisterForm(passSeries, passNumber);

            registerForm.Visible = true;

            registerForm.FormClosed += RegisterFormClosed;
        }

        private void RegisterFormClosed(object sender, FormClosedEventArgs formClosedEventArgs)
        {
            DeactivateControls();
            ArrangeItems();
        }

        private void RegisterPassForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            DatabaseHelper.Disconnect();
        }

        private void dormTextBox_TextChanged(object sender, EventArgs e)
        {
            DeactivateControls();
        }
    }
}
